package common

import "math"

type Vector2D struct {
	x float64
	y float64



}

//取坐标长度
func (vec *Vector2D) Length() float64{
	num := vec.x * vec.x + vec.y * vec.y
	return math.Sqrt(num)
}

func (vec *Vector2D) LengthSq() float64{
	num := vec.x * vec.x + vec.y * vec.y
	return num
}

//计算点积
func (vec *Vector2D) Dot(vec2 *Vector2D) float64{
	return vec.x * vec2.x + vec.y * vec2.y
}

//y轴变换
func (vec *Vector2D) Prep(){
	vec.y = -vec.y
}

//取2点距离
func (vec *Vector2D) Distance(vec2 *Vector2D) float64{
	dy := vec2.y - vec.y
	dx := vec2.x - vec.x
	return math.Sqrt(dy * dy + dx * dx)
}

func (vec *Vector2D) DistanceSq(vec2 *Vector2D) float64{
	dy := vec2.y - vec.y
	dx := vec2.x - vec.x
	return dy * dy + dx * dx
}

//标准向量
func (vec *Vector2D) Normalize(){
	len := vec.Length()
	vec.x = vec.x / len
	vec.y = vec.y / len
}

//最大
func (vec *Vector2D) Truncate(max float64){
	if vec.Length() > max {
		vec.Normalize()
		vec.x = vec.x * max
		vec.y = vec.y * max
	}
}

//
func (vec *Vector2D) Add(vec2 *Vector2D){
	vec.x = vec.x + vec2.x
	vec.y = vec.y + vec2.y
}

func (vec *Vector2D) Sub(vec2 *Vector2D){
	vec.x = vec.x - vec2.x
	vec.y = vec.y - vec2.y
}

func (vec *Vector2D) Ride(val float64){
	vec.x = vec.x * val
	vec.y = vec.y * val
}

func (vec *Vector2D) Except(val float64){
	vec.x = vec.x / val
	vec.y = vec.y / val
}

//反向向量
func (vec *Vector2D) GetReverse() *Vector2D{
	return &Vector2D{-vec.x, -vec.y}
}